﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;
using System.Data;
using System.Data.SqlClient;

namespace SHNOPData.common
{
    public static class ConvertUtil
    {
        /// <summary>
        /// 时间格式：yyyy-MM-dd HH:mm:ss
        /// </summary>
        public static readonly string DateFormat = "yyyy-MM-dd HH:mm:ss";
        /// <summary>
        /// 时间格式：yyyy-MM-dd
        /// </summary>
        public static readonly string DateFormat_YMD = "yyyy-MM-dd";
        /// <summary>
        /// 时间格式：HHmm
        /// </summary>
        public static readonly string DateFormat_HM = "HHmm";
        /// <summary>
        /// MS SQL Server 2005 datetime类型支持的最小时间
        /// </summary>
        public static readonly DateTime SQLServer2005MinValue = new DateTime(1953, 1, 1);
        /// <summary>
        /// MS SQL Server 2005 datetime类型支持的最大时间
        /// </summary>
        public static readonly DateTime SQLServer2005MaxValue = new DateTime(9999, 12, 31, 23, 59, 59, 997);

        public static string Int2Str(object obj)
        {
            if (obj == null || obj.ToString().Trim().Equals(""))
            {
                return null;
            }
            string str = obj.ToString().Trim();
            int ret = 0;
            if (Int32.TryParse(str, out ret))
            {
                return str;
            }
            return null;
        }

        public static string Obj2Str(object str)
        {
            if ((str == null) || str.ToString().ToLower().Equals("null"))
            {
                return "";
            }
            return str.ToString();
        }

        public static bool Obj2Bool(object obj)
        {
            string str = Obj2Str(obj).Trim();
            bool ret = false;
            bool.TryParse(str, out ret);
            return ret;
        }

        public static bool Obj2Bool(object obj, string strTrue)
        {
            string str = Obj2Str(obj).Trim();
            if (str.Equals(strTrue))
            {
                return true;
            }
            bool ret = false;
            bool.TryParse(str, out ret);
            return ret;
        }

        public static int Obj2Bit(object obj, string strTrue)
        {
            string str = Obj2Str(obj).Trim();
            if (strTrue != null && str.Equals(strTrue))
            {
                return 1;
            }
            return 0;
        }

        public static bool StringToBool(object obj)
        {
            string str = Obj2Str(obj).Trim();
            if (str.Equals("1"))
            {
                return true;
            }
            if (str.ToLower().Equals("true"))
            {
                return true;
            }
            return false;
        }

        public static bool StringToBool(object obj, string strTrue)
        {
            string str = Obj2Str(obj).Trim();
            if (str.Equals(strTrue))
            {
                return true;
            }
            return false;
        }

        public static int StringIdToInt(object obj)
        {
            string str = Obj2Str(obj).Trim();
            int ret = 0;
            if (Int32.TryParse(str, out ret))
            {
                return ret;
            }
            return -1;
        }

        public static int StringToInt(object obj)
        {
            string str = Obj2Str(obj).Trim();
            int ret = 0;
            if (Int32.TryParse(str, out ret))
            {
                return ret;
            }
            return ret;
        }

        public static long StringToLong(object obj)
        {
            string str = Obj2Str(obj).Trim();
            long ret = 0;
            if (Int64.TryParse(str, out ret))
            {
                return ret;
            }
            return ret;
        }

        public static double StringToDouble(object obj)
        {
            string str = Obj2Str(obj).Trim();
            double ret = 0.0d;
            if (Double.TryParse(str, out ret))
            {
                return ret;
            }
            return ret;
        }

        public static float StringToFloat(object obj)
        {
            string str = Obj2Str(obj).Trim();
            float ret = 0.0f;
            if (Single.TryParse(str, out ret))
            {
                return ret;
            }
            return ret;
        }

        public static DateTime StringToDate(object obj)
        {
            string str = Obj2Str(obj).Trim();
            DateTime date = new DateTime();
            if (DateTime.TryParse(str, out date))
            {
                return date;
            }
            return date;
        }

        public static DateTime StringToTime(object obj, string format)
        {
            string str = Obj2Str(obj).Trim();
            DateTime time;
            if (DateTime.TryParseExact(str, format, new CultureInfo("zh-CN"), DateTimeStyles.None, out time))
            {
                return time;
            }
            return time;
        }

        public static string FormatDate(object obj, string format)
        {
            if (obj == null)
            {
                return "";
            }
            string str = Obj2Str(obj).Trim();
            if (str.Equals(""))
            {
                return "";
            }
            DateTime date = new DateTime();
            if (DateTime.TryParse(str, out date))
            {
                return date.ToString(format);
            }
            return "";
        }

        public static string FormatSQLServerDateTime(object obj, string format)
        {
            if (obj == null)
            {
                return "";
            }
            string str = Obj2Str(obj).Trim();
            if (str.Equals(""))
            {
                return "";
            }
            DateTime date = new DateTime();
            if (DateTime.TryParse(str, out date))
            {
                if (date < SQLServer2005MinValue || date > SQLServer2005MaxValue)
                {
                    return "";
                }
                return date.ToString(format);
            }
            return "";
        }

        public static string MakeSqlStringCannotNULL(string str)
        {
            return MakeSqlString(str == string.Empty ? " " : str);
        }

        public static string MakeSqlString(string str)
        {
            if (str == null || str == string.Empty)
            {
                return "NULL";
            }
            else
            {
                return "'" + str.Replace("'", "''") + "'";
            }
        }

        public static string MakeSqlDateTime(object obj, string format)
        {
            if(obj == null)
            {
                return "NULL";
            }
            DateTime date = StringToDate(obj);
            if (date.CompareTo(SQLServer2005MinValue) < 0)
            {
                return "NULL";
            }
            return "'" + date.ToString(format) + "'";
        }

        public static string MakeSqlDateTime(object obj)
        {
            if (obj == null)
            {
                return "NULL";
            }
            DateTime date = StringToDate(obj);
            if (date.CompareTo(SQLServer2005MinValue) < 0)
            {
                return "NULL";
            }
            return "'" + date.ToString(DateFormat) + "'";
        }

        public static string MakeSqlGuidString(Guid gid)
        {
            return MakeSqlString((gid == Guid.Empty) ? string.Empty : gid.ToString());
        }

        public static Guid StringToGuid(Object obj)
        {
            Guid ret = Guid.Empty;
            if (obj == null || obj.ToString().Trim().Equals(""))
            {
                return ret;
            }
            try
            {
                ret = new Guid(obj.ToString().Trim());
            }
            catch(Exception)
            {

            }
            return ret;
        }

        public static SqlParameter MakeSqlParameter(string param_name, string val)
        {
            SqlParameter rst = new SqlParameter(param_name, val);
            return rst;
        }

        public static SqlParameter MakeSqlParameter(string param_name, Guid val)
        {
            SqlParameter rst = new SqlParameter(param_name, val);
            return rst;
        }

        public static int BitToInt(Object obj)
        {
            if (obj == null || obj.ToString().Trim().Equals(""))
            {
                return -1;
            }
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public static string BitToString(int value)
        {
            if (value == 0)
            {
                return "否";
            }
            else if (value == 1)
            {
                return "是";
            }
            else
            {
                return string.Empty;
            }
        }

        public static string MakeSqlBit(int value)
        {
            if (value == 0)
            {
                return "0";
            }
            else if (value == 1)
            {
                return "1";
            }
            else
            {
                return "NULL";
            }
        }

    }
}
